using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.Log;
using IM.Easy.LogServices.Dtos;
using IM.Easy.LogServices.IServices;

namespace IM.Easy.LogServices.Services
{
    public class UserOnlineLogService : AppServices<UserOnlineLog>, IUserOnlineLogService
    {

        public UserOnlineLogService(ISqlSugarClient db) : base(db)
        {

        }

        public UserOnlineLog Create(UserOnlineLog entity)
        {
            // TODO: 添加创建逻辑
            return Db.Insertable(entity).ExecuteReturnEntity();
        }

        public UserOnlineLog GetById(string id)
        {
            // TODO: 添加获取逻辑
            return Db.Queryable<UserOnlineLog>().InSingle(id);
        }

        public PageResponse<UserOnlineLog> GetPageList(UserOnlineLogQueryDto query)
        {
            var queryable = Db.Queryable<UserOnlineLog>();
            var exp = Expressionable.Create<UserOnlineLog>();
            exp = exp
;
            var page = queryable
                 .Where(exp.ToExpression())
                 .ToPageList<UserOnlineLog, UserOnlineLog>(query);
            return page;
        }

        /// <summary>
        /// 根据连接ID获取连接信息
        /// </summary>
        /// <param name="connectionID"></param>
        /// <returns></returns>
        public List<UserOnlineLogDto> GetUserOnlineLog(params string[] connectionID)
        {
            if (connectionID == null || connectionID.Length <= 0)
                return [];
            var q = Db.Reportable(connectionID).ToQueryable<string>()
                .LeftJoin<UserOnlineLog>((a, b) => a.ColumnName == b.ConnectionId)
                .Select((a, b) => new UserOnlineLogDto
                {
                    ConnectionId = a.ColumnName,
                }, true)
                .ToList();
            return q;
        }

        public bool Update(UserOnlineLog entity)
        {
            // TODO: 添加更新逻辑
            return Db.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params string[] Id)
        {
            // TODO: 添加删除逻辑
            return Db.Deleteable<UserOnlineLog>(Id).ExecuteCommandHasChange();
        }
    }
}
